<?php


class Appcore_Model_Object_Page extends Appcore_Model_Server_Database
								
{
	
	protected 	$_UrlRow		=	null;
	
	protected 	$_PageRow		=	null;
	
	protected 	$_MetaRow		=	null;
	
	public 		$_Elements;
	
	public		$_pid;
		
	public  	$_isHidden;
	
	public 		$_isActive;
	
	public $_ZendConfig;
	
	protected $_parentPage;
	
	public  $_version;
	
	public $_created;
	
	public  $_createdBy;
	
	public  $_urlAlias;
	
	public  $_langCode;
	
	public  $_Meta;
	
	
	public function __construct()
	{
		
		
	}
	
	public function get($pid, $lang = 'de', $isActive = true){
		
		$DbModel 	= new Appcore_Model_Server_Db_Table_Pages();
			
		$select		= $DbModel->select()->Where('pid = ?', $pid)->where('isactive = ?', $isActive); 	
		
		
		//PageRow setzen
		$this->_PageRow		= $DbModel->fetchRow($select);
		
		//UrlRow setzen wenn noch nicht gesetzt
		if($this->_UrlRow == null){
			$this->_UrlRow		= $this->_PageRow->findParentRow('Appcore_Model_Server_Db_Table_Urlalias');	
		}
		
		//MetaRow setzen
		$this->_MetaRow		= $this->_PageRow->findParentRow('Appcore_Model_Server_Db_Table_Meta');
		
		//Inhaltselemente finden und speichern
		//$this->_Elements	= $this->_PageRow->findParentRow('Appcore_Model_Server_Db_Table_Elements');
		
		
		//Attribute setzen
		$this->setAttributes();
		
		$this->_Elements	= $this->getElements($this->_pid, $this->_langCode);
		
		
		
		
		
		
		
		return $this;
	}
	
	public function getByUrlAlias($url){
		
		
		
		$DbModel 		= 	new Appcore_Model_Server_Db_Table_Urlalias();
		
		$select 		=	$DbModel->select()->Where('alias = ?', $url);
		
		$this->_UrlRow	= 	$DbModel->fetchRow($select);

		
		//Das Pageobject zu dieser Url holen
		//$pObj	= 	$this->_UrlRow->pid;
		
		$pObj	=	$this->get($this->_UrlRow->pid, $this->_UrlRow->langCode);
		
		
		return $pObj;
	}
	
	public function getChildPages(){
		
	}
	
	public function getByParentPage(){
		
	}
	
	public function getElements($pid = 3, $langCode = 'de', $hidden = 0){
		
		//ElementsModel init
		$eModel				= 	new Appcore_Model_Server_Db_Table_Elements();
		
		$select				=	$eModel->select()->where('pid = ?', $pid)->Where('lang = ?', $langCode);
		
		$elements			=	$eModel->fetchAll($select);
		
		return $elements;
	}
	
	public function setAttributes()
	{
		
		//_pid
		$this->_pid			=	$this->_PageRow->pid;
		
		//$_isHidden;
		$this->_isHidden	=	$this->_PageRow->ishidden;
	
		//$_isActive;
		$this->_isActive	=	$this->_PageRow->isactive;
		
		//$_ZendConfig;
		$this->_ZendConfig	=	array(
				'Modul'			=>	$this->_PageRow->zf_modul,
				'Controller'	=>	$this->_PageRow->zf_controller,
				'Action'		=>	$this->_PageRow->zf_action,
		);
	
		//$_parentPage;
	
		//$_version;
	
		//$_created;
	
		//$_createdBy;
	
		//$_urlAlias;
		$this->_urlAlias	=	$this->_UrlRow->alias;
	
		//$_langCode;
		$this->_langCode	=	$this->_UrlRow->langCode;
		
		//$_Meta;
		$this->_Meta		=	array(
				'Title'			=>	$this->_MetaRow->title,
				'Description'	=>	$this->_MetaRow->description,
				'Keywords'		=>	$this->_MetaRow->keywords,
		);
	}
}